// mircoApp/activityClock/clockMember/clockMember.js
const app = getApp();
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    query:{
      admin_type:0,
      page:1,
    },
    actQuery:{
      status:'',
      title:'',
      page:1,
    },
    totalPage:0,
    isLoadMore:true,
    memberTab:[
      {
        name:'参与的',
        val:0
      },
    ],
    clockList:[],
    iPnoneBottomBol:false,
    calendarInfo:{},
    activityList:[],
    isAdmin:false,
    imgPath:app.globalData.imgPath
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initData();
    let yz_basic_info = wx.getStorageSync("yz_basic_info");
    let plugin_name =  yz_basic_info.plugin_setting?.check_in_rebate?.plugin_name;
    wx.setNavigationBarTitle({
      title: plugin_name,
    })
    this.getiPnoneBottom();
    this.getMember();
    this.clockListData();
  },
  initData() {
    this.setData({
      'query.page':1,
      'query.admin_type':0,
      isLoadMore:true,
      clockList:[],
    })
    
  },
  memberTabOn(event) {
    let {name} = event.detail;
    if(name == 0 && this.data.isAdmin) {
     this.setData({
       'query.admin_type':1,
       isLoadMore:true,
       activityList:[],
       active:name,
       'actQuery.page':1,
     })
     this.activityListData();
    } else {
      this.setData({
        isLoadMore:true,
        'query.page':1,
          active:name,
        'query.admin_type':0,
        clockList:[]
      })
      this.clockListData();
    }
    
  },
  getMember() { //未购买商品的商品详情
    let urlStr = app.getNetAddresss("plugin.check-in-rebate.frontend.participate-in.get-Info");
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        var res = resdata.data;
        let data = res.data;
        if (res.result == 1) {
          let infoMember = {
            priceName:'退回金额（元）',
            priceVal:data.member?.reward_price || '0',
            couponName:'优惠券（张）',
            couponVal:data.member?.coupon_num || '0'
          };
          this.data.calendarInfo = {...infoMember,...data.member};
          if(data.member.is_admin) {
            let memberTab = [
              {
                name:'管理的',
                val:0,
              },
              {
                name:'参与的',
                val:1,
              },
            ];
            this.data.memberTab = memberTab;
            this.data.query.admin_type = 1;
            this.setData({
              'actQuery.page':1,
              isAdmin:data.member.is_admin,
              activityList:[],
            })
            this.activityListData();
          }
          
          this.setData({
            calendarInfo:this.data.calendarInfo,
            memberTab:this.data.memberTab,
            'query.admin_type':this.data.query.admin_type
          })
          let tab_component = this.selectComponent('.tab_component');
          tab_component.resize();
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  clockListData() {
    let urlStr = app.getNetAddresss("plugin.check-in-rebate.frontend.participate-in.get-participateIn-list");
    app._postNetWork({
      url: urlStr,
      data:this.data.query,
      success:  (resdata) => {
        let res = resdata.data;
        let data = res.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.totalPage = data.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            clockList: data.data,
          })
        } else {
          this.data.query.page = this.data.query.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  activityListData() {
    let urlStr = app.getNetAddresss("plugin.check-in-rebate.frontend.index.get-base-info");
      app._postNetWork({
        url: urlStr,
        data:{search:this.data.actQuery},
        success:  (resdata) => {
          let res = resdata.data;
          let data = res.data;
          wx.hideLoading();
          wx.hideNavigationBarLoading();//隐藏导航条加载动画
          //停止下拉刷新
          wx.stopPullDownRefresh();
          if (res.result == 1) {
            let {activity} = data;
            this.data.actTotalPage = activity.last_page;
            if (!this.data.actTotalPage) {
              this.data.actTotalPage = 0;
            }
            this.setData({
              activityList: activity.data,
            })
          } else {
            this.data.actQuery.page = this.data.actQuery.page - 1;
            this.data.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
  },
   //获取更多数据
   actMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.actQuery.page >= this.data.actTotalPage) {
      return;
    } else {
      this.data.actQuery.page = this.data.actQuery.page + 1;
      let urlStr = app.getNetAddresss("plugin.check-in-rebate.frontend.index.get-base-info");
      app._postNetWork({
        url: urlStr,
        data: this.data.actQuery,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data;
          let {activity} = list;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              activityList: this.data.activityList.concat(activity.data),
            })
          } else {
            this.data.actQuery.page = this.data.actQuery.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
   //获取更多数据
   getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.query.page >= this.data.totalPage) {
      return;
    } else {
      this.data.query.page = this.data.query.page + 1;
      let urlStr = app.getNetAddresss("plugin.check-in-rebate.frontend.participate-in.get-participateIn-list");
      app._postNetWork({
        url: urlStr,
        data: this.data.query,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data;
          console.log(list);
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              clockList: this.data.clockList.concat(list.data),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  getiPnoneBottom() {
    let that = this;
    try {
      let value = wx.getStorageSync('iPhoneBottom');
      if (value) {
        that.setData({
          iPnoneBottomBol: true
        });
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.active == 0 && this.data.isAdmin) {
      if (this.data.isLoadMore) {
        this.actMoreData()
      } else {
        console.log('没有更多了');
      }
    } else {
      if (this.data.isLoadMore) {
        this.getMoreData();
      } else {
        console.log('没有更多了');
      }
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: "打卡日历",
      path: "mircoApp/activityClock/clockMember/clockMember",
    };
  }
})